/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package miniproject;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Burim Sadriu
 */
public final class createPanel extends JPanel implements ActionListener{
 private JTextField userNameField;
 private JTextField passwordField;
 private JTextField adminField;
 private JTextField prodNameField;
 private JTextField priceField;
 private JTextField shoes;
 private JTextField sweater;
 private JTextField jacket;
 private JTextField pillow;
 private JTextField scentedCandles;
 private JTextField shoesStock;
 private JTextField sweaterStock;
 private JTextField jacketStock;
 private JTextField nbrOfProducts;
 private JTextField productStock;
 private JTextField nbrOfMembers;
 private JTextField memberStock;
 //public JTextField totalPriceStock;
 //*******
 private JButton enter;
 private JButton admin;
 private JButton buy;
 private JButton delete;
 private JButton add;
 private JButton exit;
 private JButton deleteCustomer;
 private JButton deleteProduct;
 private JButton addProduct;
 private JButton newUser;
 private JButton deleteProd;
 private JButton viewData;
 private JButton back;
 private JButton backToLogin;
 //******
 private JPanel loginPanel;
 private JPanel productPanel;
 private JPanel adminPanel;
 private JPanel prodInfoPanel;
 private JPanel custInfoPanel;
 private JPanel viewDataPanel;
 //******
 private JCheckBox shoesCheckBox;
 private JCheckBox sweatherCheckBox;
 private JCheckBox jacketCheckBox;
 private JCheckBox pillowCheckBox;
 private JCheckBox scentedCandelsCheckBox;
 //private ButtonGroup group;
 //*******
 private JLabel userNameText;
 private JLabel passwordText;
 private JLabel loginText;
 private JLabel productText;
 private JLabel productName;
 private JLabel productPrice;
 //*******
 private int a;
 frame frame = new frame();
 dataBase dBase = new dataBase();

 public createPanel() {
     createLoginPanel();
 }

 public void createLoginPanel() {
     loginPanel = new JPanel();
     Color color = new Color(135,180,230);
     Color buttonColor = new Color(80,125,5);
     loginPanel.setBackground(color);
     loginPanel.setLayout(null);//can't change button, field and text position without this line!

     loginText = new JLabel("Welcome! Log in to buy!");
     loginText.setFont(new Font("Monotype Corsiva",20,23));
     loginText.setBounds(100, 20, 270, 25);
     loginPanel.add(loginText);

     //create enter button and add it to the loginPanel.
     enter = new JButton("Log in / Add");
     enter.setBounds(164, 210, 100, 30);
     loginPanel.add(enter);

     admin = new JButton("Admin");
     admin.setBounds(35, 325, 80, 30);
     admin.setForeground(buttonColor);
     loginPanel.add(admin);

     newUser = new JButton("New User");
     newUser.setBounds(190, 320, 90, 30);
     //loginPanel.add(newUser);

     //create password field and add it to the loginPanel.
     passwordField = new JPasswordField("", 10);
     passwordField.setBounds(165, 160, 100, 25);
     loginPanel.add(passwordField);

     userNameField = new JTextField("", 10);
     userNameField.setBounds(165, 115, 100, 25);
     loginPanel.add(userNameField);

     adminField = new JPasswordField("", 10);
     adminField.setBounds(27, 291, 100, 25);
     adminField.setForeground(buttonColor);
     loginPanel.add(adminField);

     userNameText = new JLabel("User name:");
     userNameText.setFont(new Font("Neue Helvetica",10,17));
     userNameText.setBounds(80, 116, 100, 20);
     loginPanel.add(userNameText);

     passwordText = new JLabel("Password:");
     passwordText.setFont(new Font("Neue Helvetica",10,17));
     passwordText.setBounds(87, 162, 100, 20);
     loginPanel.add(passwordText);

     frame.add(loginPanel);//add the login panel that we just created to the frame window in frame class.

     enter.addActionListener(this);//listen if we click on enter button.
     admin.addActionListener(this);
     adminField.addActionListener(this);
     userNameField.addActionListener(this);
     passwordField.addActionListener(this);
 }
 public void createProductPanel() {
     productPanel = new JPanel();
     //contentPane.remove(loginPanel);
     frame.remove(loginPanel);
     Color color = new Color(150,180,200);
     productPanel.setBackground(color);
     productPanel.setLayout(null);

     productText = new JLabel("Products");
     productText.setFont(new Font("Monotype Corsiva",20,23));
     productText.setBounds(150, 12, 270, 25);
     productPanel.add(productText);

     buy = new JButton("Buy");
     buy.setBounds(250, 320, 80, 30);
     productPanel.add(buy);

     exit = new JButton("Exit");
     exit.setBounds(50, 320, 80, 30);
     productPanel.add(exit);


     shoesCheckBox = new JCheckBox("", false);
     shoesCheckBox.setBounds(75, 60, 20, 50);
     shoesCheckBox.setBackground(color);
     productPanel.add(shoesCheckBox);

     sweatherCheckBox = new JCheckBox("", false);
     sweatherCheckBox.setBounds(75, 100, 20, 50);
     sweatherCheckBox.setBackground(color);
     productPanel.add(sweatherCheckBox);

     jacketCheckBox = new JCheckBox("", false);
     jacketCheckBox.setBounds(75, 140, 20, 50);
     jacketCheckBox.setBackground(color);
     productPanel.add(jacketCheckBox);

     pillowCheckBox = new JCheckBox("", false);
     pillowCheckBox.setBounds(75, 180, 20, 50);
     pillowCheckBox.setBackground(color);
     productPanel.add(pillowCheckBox);

     scentedCandelsCheckBox = new JCheckBox("", false);
     scentedCandelsCheckBox.setBounds(75, 220, 20, 50);
     scentedCandelsCheckBox.setBackground(color);
     productPanel.add(scentedCandelsCheckBox);

     /*
     group = new ButtonGroup();
     group.add(shoesCheckBox);
     group.add(sweatherCheckBox);
     group.add(jacketCheckBox);
     group.add(pillowCheckBox);
     group.add(scentedCandelsCheckBox);
     */
     frame.add(productPanel);//add the product panel that we just created to the frame window in frame class.

     shoesCheckBox.addActionListener(this);
     sweatherCheckBox.addActionListener(this);
     jacketCheckBox.addActionListener(this);
     pillowCheckBox.addActionListener(this);
     scentedCandelsCheckBox.addActionListener(this);

     buy.addActionListener(this);
     exit.addActionListener(this);
     frame.setVisible(true);
     //*********************************************************************************************************************
     shoes = new JTextField("Nike shoes");
     shoes.setBounds(110, 75, 170, 20);
     shoes.setEditable(false);
     productPanel.add(shoes);

     sweater = new JTextField("Sweater");
     sweater.setBounds(110, 115, 170, 20);
     sweater.setEditable(false);
     productPanel.add(sweater);

     jacket = new JTextField("Jacket");
     jacket.setBounds(110, 155, 170, 20);
     jacket.setEditable(false);
     productPanel.add(jacket);

     pillow = new JTextField("Pillow");
     pillow.setBounds(110, 195, 170, 20);
     pillow.setEditable(false);
     productPanel.add(pillow);

     scentedCandles = new JTextField("Scented Candles - Orange");
     scentedCandles.setBounds(110, 235,170, 20);
     scentedCandles.setEditable(false);
     productPanel.add(scentedCandles);

     /*
     totalPriceStock = new JTextField("Total");
     totalPriceStock.setBounds(210, 295,60, 20);
     totalPriceStock.setEditable(false);
     productPanel.add(totalPriceStock);
     
     
     totalPriceStock = new JTextField("Total: 3500 SEK");
     totalPriceStock.setBounds(230, 290,120, 20);
     totalPriceStock.setEditable(false);
     productPanel.add(totalPriceStock);
      * */

 }
 public void createAdminPanel() {
     adminPanel = new JPanel();
     //contentPane.remove(loginPanel);
     frame.remove(loginPanel);
     Color color = new Color(160,205,130);
     Color buttonColor = new Color(80,125,5);
     adminPanel.setBackground(color);
     adminPanel.setLayout(null);

     viewData = new JButton("View data");
     viewData.setBounds(120, 60, 160, 30);
     viewData.setForeground(buttonColor);
     adminPanel.add(viewData);

     deleteCustomer = new JButton("Delete customer");
     deleteCustomer.setBounds(120, 240, 160, 30);
     deleteCustomer.setForeground(buttonColor);
     adminPanel.add(deleteCustomer);

     deleteProduct = new JButton("Delete product");
     deleteProduct.setBounds(120, 180, 160, 30);
     deleteProduct.setForeground(buttonColor);
     adminPanel.add(deleteProduct);

     addProduct = new JButton("Add product");
     addProduct.setBounds(120, 120, 160, 30);
     addProduct.setForeground(buttonColor);
     adminPanel.add(addProduct);

     backToLogin = new JButton("Login Window");
     backToLogin.setBounds(137, 328, 120, 30);
     backToLogin.setForeground(buttonColor);
     adminPanel.add(backToLogin);

     frame.add(adminPanel);

     deleteCustomer.addActionListener(this);
     addProduct.addActionListener(this);
     deleteProduct.addActionListener(this);
     viewData.addActionListener(this);
     backToLogin.addActionListener(this);
     frame.setVisible(true);
 }
 public void viewNbrOfData() {
     a = 1;
     viewDataPanel = new JPanel();
     frame.remove(adminPanel);
     Color color = new Color(160,205,130);
     Color buttonColor = new Color(80,125,5);
     viewDataPanel.setBackground(color);
     viewDataPanel.setLayout(null);
     
     back = new JButton("Go Back");
     back.setBounds(140, 310, 90, 30);
     back.setForeground(buttonColor);
     viewDataPanel.add(back);
     
     nbrOfProducts = new JTextField("Products");
     nbrOfProducts.setBounds(100, 120, 170, 20);
     nbrOfProducts.setEditable(false);
     viewDataPanel.add(nbrOfProducts);

     int nbrOfprod = dBase.nbrOfProducts();
     String s1 = Integer.toString(nbrOfprod);
     productStock = new JTextField(s1);
     productStock.setBounds(300, 120, 30, 20);
     productStock.setEditable(false);
     viewDataPanel.add(productStock);

     nbrOfMembers = new JTextField("Members");
     nbrOfMembers.setBounds(100, 170,170, 20);
     nbrOfMembers.setEditable(false);
     viewDataPanel.add(nbrOfMembers);
     
     int nbrOfCust = dBase.nbrOfCustomers();
     String s2 = Integer.toString(nbrOfCust);

     memberStock = new JTextField(s2);
     memberStock.setBounds(300, 170,30, 20);
     memberStock.setEditable(false);
     viewDataPanel.add(memberStock);

      back.addActionListener(this);

      frame.add(viewDataPanel);
      frame.setVisible(true);
 }
 public void addProdPanel() {
     a = 2;
     prodInfoPanel = new JPanel();
     frame.remove(adminPanel);
     Color color = new Color(160,205,130);
     Color buttonColor = new Color(80,125,5);

     prodInfoPanel.setBackground(color);
     prodInfoPanel.setLayout(null);

     back = new JButton("Go Back");
     back.setBounds(50, 310, 90, 30);
     back.setForeground(buttonColor);
     prodInfoPanel.add(back);

     add = new JButton("Add");
     add.setBounds(240, 310, 90, 30);
     add.setForeground(buttonColor);
     prodInfoPanel.add(add);

     prodNameField = new JTextField("", 10);
     prodNameField.setBounds(150, 130, 100, 25);
     prodInfoPanel.add(prodNameField);

     priceField = new JTextField("", 10);
     priceField.setBounds(150, 180, 100, 25);
     prodInfoPanel.add(priceField);

     productName = new JLabel("Name:");
     productName.setFont(new Font("Monotype Corsiva",10,20));
     productName.setBounds(90, 133, 100, 20);
     prodInfoPanel.add(productName);

     productPrice = new JLabel("Price:");
     productPrice.setFont(new Font("Monotype Corsiva",10,20));
     productPrice.setBounds(90, 193, 100, 20);
     prodInfoPanel.add(productPrice);

     frame.add(prodInfoPanel);

     prodNameField.addActionListener(this);
     priceField.addActionListener(this);
     back.addActionListener(this);
     add.addActionListener(this);
     frame.setVisible(true);
 }
 public void deleteProdPanel() {
     a = 3;
     prodInfoPanel = new JPanel();
     frame.remove(adminPanel);
     Color color = new Color(160,205,130);
     Color buttonColor = new Color(80,125,5);
     prodInfoPanel.setBackground(color);
     prodInfoPanel.setLayout(null);

     back = new JButton("Go Back");
     back.setBounds(60, 310, 90, 30);
     back.setForeground(buttonColor);
     prodInfoPanel.add(back);

     deleteProd = new JButton("Delete");
     deleteProd.setBounds(220, 310, 90, 30);
     deleteProd.setForeground(buttonColor);
     prodInfoPanel.add(deleteProd);

     prodNameField = new JTextField("", 10);
     prodNameField.setBounds(150, 140, 100, 25);
     prodInfoPanel.add(prodNameField);

     productName = new JLabel("Name:");
     productName.setFont(new Font("Monotype Corsiva",10,20));
     productName.setBounds(90, 143, 100, 20);
     prodInfoPanel.add(productName);

     frame.add(prodInfoPanel);

     prodNameField.addActionListener(this);
     back.addActionListener(this);
     deleteProd.addActionListener(this);
     frame.setVisible(true);
 }
 public void deleteCustPanel() {
     a = 4;
     custInfoPanel = new JPanel();
     //contentPane.remove(loginPanel);
     frame.remove(adminPanel);
     Color color = new Color(160,205,130);
     Color buttonColor = new Color(80,125,5);
     custInfoPanel.setBackground(color);
     custInfoPanel.setLayout(null);

     back = new JButton("Go Back");
     back.setBounds(60, 310, 90, 30);
     back.setForeground(buttonColor);
     custInfoPanel.add(back);

     delete = new JButton("Delete");
     delete.setBounds(220, 310, 90, 30);
     delete.setForeground(buttonColor);
     custInfoPanel.add(delete);

     passwordField = new JPasswordField("", 10);
     passwordField.setBounds(150, 140, 100, 25);
     custInfoPanel.add(passwordField);

     passwordText = new JLabel("Password:");
     passwordText.setFont(new Font("Monotype Corsiva",10,20));
     passwordText.setBounds(65, 143, 100, 20);
     custInfoPanel.add(passwordText);

     frame.add(custInfoPanel);

     passwordField.addActionListener(this);
     back.addActionListener(this);
     delete.addActionListener(this);
     frame.setVisible(true);
 }
 public void actionPerformed(ActionEvent event) {
     String string = "";
     if (event.getSource() == userNameField) {
         string = String.format(event.getActionCommand());
     }
     if (event.getSource() == productName) {
         string = String.format(event.getActionCommand());
     }
     if (event.getSource() == productPrice) {
         string = String.format(event.getActionCommand());
     }
     if (event.getSource() == passwordField) {
         string = String.format(event.getActionCommand());
     }
     else if(event.getSource() == enter) {
         //System.out.println("Enter button OK");
         String userName = userNameField.getText();
         String password = passwordField.getText();
         if(!userName.equals("") && !password.equals("") ) {
             dBase.validateCustomer(userName, password);
             createProductPanel();
             //dBase.insertCostumers(userName, password);
         }
     }
     else if(event.getSource() == buy) {
         String dataName;
         if(shoesCheckBox.isSelected()==true) {
             dataName="nike";
             dBase.deleteProducts(dataName);
         }
         if(sweatherCheckBox.isSelected()==true) {
             System.out.println("Shoes box selectet");
             dataName="sweater";
             dBase.deleteProducts(dataName);
             //System.out.println("Thank you! You bought sweater");
         }
         if(jacketCheckBox.isSelected()==true) {
             //System.out.println("Jacket box selectet");
             dataName="jacket";
             dBase.deleteProducts(dataName);
         }
         if(pillowCheckBox.isSelected()==true) {
             //System.out.println("Pillow box selectet");
             dataName="pillow";
             dBase.deleteProducts(dataName);
         }
         if(scentedCandelsCheckBox.isSelected()==true) {
             //System.out.println("Scented candels box selectet");
             dataName="candles";
             dBase.deleteProducts(dataName);
         }
         System.out.println("Thank you!");
         //System.out.println("Buy button OK");
         productPanel.setVisible(false);
         createLoginPanel();
     }
     else if(event.getSource() == exit) {
         System.exit(0);
     }
     else if(event.getSource() == admin) {
         String adminPassword = adminField.getText();//admin password = admin.
         //createAdminPanel();
         if(adminPassword.equals("admin")) {//if password is correct (admin) create adminPanel.
             createAdminPanel();
         }
     }
     else if(event.getSource() == deleteCustomer) {
         deleteCustPanel();
     }
     else if(event.getSource() == deleteProduct) {
         deleteProdPanel();
     }
     else if(event.getSource() == deleteProd) {
         String name = prodNameField.getText();
         if(!name.equals("")) {
             dBase.deleteProducts(name);
             System.out.println("Product with name "+name+" deletet");
             frame.remove(prodInfoPanel);
             createAdminPanel();
         }
     }
     else if(event.getSource() == addProduct) {
         addProdPanel();
     }
     else if(event.getSource() == back && a==2) {
         frame.remove(prodInfoPanel);
         createAdminPanel();
     }
     else if(event.getSource() == back && a == 3) {
         frame.remove(prodInfoPanel);
         createAdminPanel();
     }
     else if(event.getSource() == back && a==1) {
         frame.remove(viewDataPanel);
         createAdminPanel();
     }
     else if(event.getSource() == back && a==4) {
         frame.remove(custInfoPanel);
         createAdminPanel();
     }
     else if(event.getSource() == back && a==4) {
         frame.remove(viewDataPanel);
         createAdminPanel();
     }
     else if(event.getSource() == backToLogin) {
         adminPanel.setVisible(false);
         createLoginPanel();
     }
     else if(event.getSource() == add) {
         String name = prodNameField.getText();
         String price = priceField.getText();
         //int price2 = Integer.parseInt(price);
         //System.out.println( price2);
         if(!name.equals("") && !price.equals("") ) {//if name field and price field are not empty do something, otherwise stay there.
             int price2 = Integer.parseInt(price);
             dBase.insertProducts(name, price2);
             System.out.println("Product added");
             prodInfoPanel.setVisible(false);
             createAdminPanel();
         }
     }
     else if(event.getSource() == delete) {
         String pass = passwordField.getText();
         if(!pass.equals("") ) {//if password field is not empty do something, otherwise stay there.
             dBase.deleteCustomers(pass);
             custInfoPanel.setVisible(false);
             System.out.println("Customer with password "+pass+" is deleted");
             createAdminPanel();
         }
     }
     else if(event.getSource() == viewData) {
         viewNbrOfData();
         dBase.printProducts();
         System.out.println();
         dBase.printCustomers();
         System.out.println();
     }
 }
}
